import os
import gtk
import gtk.glade
import gobject
import subprocess
from BuildWizException import BuildWizException

class DlgShowMountedDevices:
    def __init__( self, title, wizard):
        self.__wiz = wizard
        self.__devmounts = []
        self.__choosenDevice = ()
        self.__glade = self.__wiz.getPreferences().getGUIFile( "mounteddevices")
        if not os.path.isfile( self.__glade):
            raise BuildWizException( "DlgShowMountedDevices: libglade could not open - [%s]" % self.__glade)
        self.__xml = gtk.glade.XML( self.__glade)
        if self.__xml:
            self.__win = self.__xml.get_widget( "DlgShowMountedDevices")
            self.__win.set_title( title)
            self.__win.connect( "destroy", self.__onCancel)
            self.__handlers = { "on_btnOK_clicked": self.__onOK
                              , "on_btnCancel_clicked": self.__onCancel 
                              , "on_btnRefresh_clicked": self.__onRefresh }
            self.__xml.signal_autoconnect( self.__handlers)
            self.__treeview = self.__xml.get_widget( "treeview")
            self.__btnOK = self.__xml.get_widget( "btnOK")
            self.__btnCancel = self.__xml.get_widget( "btnCancel")

            self.__store = gtk.TreeStore( gobject.TYPE_STRING, gobject.TYPE_STRING)
            if self.__treeview:
                self.__treeview.set_model( self.__store) 

                rendererDevice = gtk.CellRendererText()
                rendererDevice.set_property( 'editable', False)
                columnDevice = gtk.TreeViewColumn( "DEVICE"
                                                 , rendererDevice
                                                 , text=0)
                rendererMounted = gtk.CellRendererText()
                rendererMounted.set_property( 'editable', False)
                columnMountedPoint = gtk.TreeViewColumn( "MOUNTED POINT"
                                                       , rendererMounted
                                                       , text=1)
                self.__treeview.append_column( columnDevice)
                self.__treeview.append_column( columnMountedPoint)
                self.__treeview.set_search_column( 0)
                columnDevice.set_sort_column_id( 0)
                self.__scan()
                self.__insertItems()

    def __scan( self):
        proc = subprocess.Popen( "mount", shell=True
                               , stdin=subprocess.PIPE
                               , stdout=subprocess.PIPE
                               , stderr=subprocess.STDOUT) # send errors to stdout
        data = proc.stdout.readline()
        while data != "":
            data = proc.stdout.readline()
            if data != "":
                self.__devmounts += [self.__getDeviceAndMountPoint( data)]


    def __insertItems( self):
        self.__store.clear()
        for item in self.__devmounts:
            self.__store.append( None, (item[ 0], item[ 1]))

    def __onRefresh( self, btn):
        self.__store.clear()
        self.__devmounts = []
        self.__scan()
        self.__insertItems()

    def run( self):
        return self.__win.run()

    def __onCancel( self, btn):
        self.__win.destroy()
        return gtk.RESPONSE_CANCEL

    def __onOK( self, btn):
        selection = self.__treeview.get_selection()
        model, modelIt, = selection.get_selected()
        if modelIt >= 0:
            path = model.get_path( modelIt)
            self.__choosenDevice = ( model[ path][ 0], model[ path][ 1])
            print "DlgShowMountedDevices.__onOK(): self.__choosenDevice = ", self.__choosenDevice

        self.__win.destroy()
        return gtk.RESPONSE_OK

    def getChoosenDevice( self):
        return self.__choosenDevice

    def __getDeviceAndMountPoint( self, line):
        slices = line.split()
        device = slices[ 0]
        mountpoint = ""
        if os.path.isdir( slices[ 1]):
            mountpoint = slices[ 1]
        elif os.path.isdir( slices[ 2]):
            mountpoint = slices[ 2]
        return (device, mountpoint)

